//
//  TLMallWaterfallFlowCollectionViewCell.m
//  WXReader
//
//  Created by huaya on 2025/1/13.
//  Copyright © 2025 Andrew. All rights reserved.
//

#import "TLMallWaterfallFlowCollectionViewCell.h"

@interface TLMallWaterfallFlowCollectionViewCell ()
@property (nonatomic, strong) UIImageView *bookImgView;
@property (nonatomic, strong) UIImageView *layoutImgView;
@property (nonatomic, strong) UIImageView *favImgView;
@property (nonatomic, strong) UILabel *collectNumLab;
@property (nonatomic, strong) UILabel *bookNameLab;
@property (nonatomic, strong) UILabel *bookDescLab;

@end

@implementation TLMallWaterfallFlowCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}
- (void)setupSubviews{
    [self.contentView setBackgroundColor:[UIColor whiteColor]];
    [self.contentView addSubview:self.bookImgView];
    [self.bookImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-104);
    }];
    
    [self.contentView addSubview:self.layoutImgView];
    [self.layoutImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.bottom.equalTo(self.bookImgView.mas_bottom);
        make.height.offset(40);
    }];
    
    [self.contentView addSubview:self.collectNumLab];
    [self.collectNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).offset(-10);
        make.bottom.equalTo(self.layoutImgView.mas_bottom).offset(-6);
    }];

    [self.contentView addSubview:self.favImgView];
    [self.favImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(15, 15));
        make.right.equalTo(self.collectNumLab.mas_left).offset(-5);
        make.centerY.equalTo(self.collectNumLab.mas_centerY);
    }];
    
    [self.contentView addSubview:self.bookNameLab];
    [self.bookNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(12);
        make.right.equalTo(self.contentView.mas_right).offset(-12);
        make.height.offset(15);
        make.top.equalTo(self.bookImgView.mas_bottom).offset(4);
    }];
    
    [self.contentView addSubview:self.bookDescLab];
    [self.bookDescLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(12);
        make.right.equalTo(self.contentView.mas_right).offset(-12);
        make.height.offset(40);
        make.top.equalTo(self.bookNameLab.mas_bottom).offset(4);
    }];
    
}

- (UILabel *)bookDescLab{
    if (!_bookDescLab) {
        _bookDescLab = [[UILabel alloc] init];
        _bookDescLab.textColor = [UIColor colorWithHexString:@"#333333"];
        _bookDescLab.font =  [UIFont systemFontOfSize:14 weight:UIFontWeightBold];
        _bookDescLab.numberOfLines = 2;
    }
    return _bookDescLab;
}
- (UILabel *)bookNameLab{
    if (!_bookNameLab) {
        _bookNameLab = [[UILabel alloc] init];
        _bookNameLab.textColor = kMainColor;
        _bookNameLab.font =  [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
    }
    return _bookNameLab;
}
- (UILabel *)collectNumLab{
    if (!_collectNumLab) {
        _collectNumLab = [[UILabel alloc] init];
        _collectNumLab.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
        _collectNumLab.font =  [UIFont systemFontOfSize:13 weight:UIFontWeightMedium];
        
    }
    return _collectNumLab;
}
- (UIImageView *)layoutImgView{
    if (!_layoutImgView) {
        _layoutImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_rectangle"]];
        
    }
    return _layoutImgView;
}
- (UIImageView *)bookImgView{
    if (!_bookImgView) {
        _bookImgView = [[UIImageView alloc] init];
    }
    return _bookImgView;
}
- (UIImageView *)favImgView{
    if (!_favImgView) {
        _favImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"favor"]];
    }
    return _favImgView;
}
- (void)setModel:(TLMallChannelModel *)model{
    if (model) {
        _model = model;
        
        _bookNameLab.text = model.worksName;
        
        _bookDescLab.text = model.desc;
        
        _collectNumLab.text = model.collectNum.stringValue;
        MJWeakSelf
        [self.bookImgView sd_setImageWithURL:[NSURL URLWithString:model.coverPic] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            
            float height =200;
            
            float maxW = (kScreenWidth-30)/2;
            
            if (MK_IS_PAD) {
                maxW = (kScreenWidth-15*5)/4;
            }
            
            if (!image) {
                
            }else{
                
                height = image.size.height/image.size.width * maxW;

                if (height<maxW*0.62) {
                    height = maxW*0.62;
                }
                if (height>maxW*1.6) {
                    height = maxW*1.6;
                }
            }
            
            int heightInt = 104;
            if ([model.tagsName isEqualToString:@""]) {
                heightInt = 70;
            }
            
            [weakSelf.bookImgView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.left.right.equalTo(self.contentView);
                make.bottom.equalTo(self.contentView.mas_bottom).offset(-heightInt);
            }];
            if (weakSelf.model.cellHeight != height+heightInt) {
                
                weakSelf.model.cellHeight = height+heightInt;
                
                if (weakSelf.reloadCellBlock) {
                    weakSelf.reloadCellBlock();
                }
            }
            
            for (UILabel *lab in self.contentView.subviews) {
                if (lab.tag>=100) {
                    [lab removeFromSuperview];
                }
            }
            
            if (![model.tagsName isEqualToString:@""]) {
                
                NSArray *tags = [model.tagsName componentsSeparatedByString:@","];
                
                [self createTagWithTags:tags];
            }
        }];
        
        
        self.layoutImgView.hidden = NO;
        self.favImgView.hidden = NO;
        self.collectNumLab.hidden = NO;
        
    }
}
- (void)createTagWithTags:(NSArray *)tags{
    
    CGFloat x = 8;
    
    for (int i=0; i<tags.count; i++) {
        
        if (i>0) {
            break;
        }
        
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(x,self.contentView.frame.size.height-21-8, 75, 20)];
        lab.textColor = [UIColor colorWithHexString:@"#999999"];
        lab.font =  [UIFont systemFontOfSize:13 weight:UIFontWeightMedium];
        lab.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        lab.text = [NSString stringWithFormat:@"  %@  ",tags[i]];
        [lab sizeToFit];
        CGRect frame = lab.frame;
        frame.size.height = 20;
        lab.frame = frame;
        
        lab.layer.masksToBounds = YES;
        lab.layer.cornerRadius = 10;
        lab.tag = 100+i;
        [self.contentView addSubview:lab];
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(x);
            make.bottom.equalTo(self.contentView.mas_bottom).offset(-12);
        }];
        x = x + frame.size.width + 8;
        
    }
    
}
- (void)setBannerModel:(TLMallBannerModel *)bannerModel{
    if (bannerModel) {
        _bannerModel = bannerModel;
        
        self.layoutImgView.hidden = YES;
        self.favImgView.hidden = YES;
        self.collectNumLab.hidden = YES;
        
        self.bookNameLab.text = bannerModel.bannerName;
        self.bookDescLab.text = bannerModel.title;
        [self.bookImgView sd_setImageWithURL:[NSURL URLWithString:bannerModel.coverPic]];
        
    }
}
@end
